package cn.djx.springsecurity.service.impl;

import cn.djx.springsecurity.entity.LoginUsers;
import cn.djx.springsecurity.entity.Users;
import cn.djx.springsecurity.mapper.UsersMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Resource
    private UsersMapper usersMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 1.构建条件
        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Users::getUserName,username);
        // 2.查询用户
        Users users=usersMapper.selectOne(queryWrapper);
        // 3.判断用户是否存在
        if(users==null){
            throw new RuntimeException("用户名密码错误");
        }
         //设置当前用户的权限  暂时写死
        //List<String> authorities = new ArrayList<>(Arrays.asList("test","admin"));
        // 3.查询用户的权限
        List<String> authorities = usersMapper.selectMenu(users.getId());
        // 4.返回用户信息
        return new LoginUsers(users,authorities);
    }

}
